/**
 * Class:   BeneficiaryEvaluationManager
 * 
 * Methods:
 * 
 *      boolean insertBeneficiaryEvaluation( int idActivity,
 *      									 BeneficiaryEvaluation beneficiaryEvaluation );
 *      
 *      BeneficiaryEvaluation searchBeneficiaryEvaluation( int activityID );
 *      
 *      boolean deleteBeneficiaryEvaluation( int activityID );
 * 
 * Remarks:
 * 
 * 		This class should not be touched by the controllers.
 * 
 * @author ( Sharmaine Lim )
 * @version ( 2013-02-28 // YYYY-MM-DD )
 */

package managers;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import models.BeneficiaryEvaluation;

public class BeneficiaryEvaluationManager extends DatabaseManager {
    
    /**
     * Constructors
     */
    
    public BeneficiaryEvaluationManager() {}
    
    
    
    /**
     *  Insert Beneficiary Evaluation
     *  	- inserts a beneficiary evaluation into the
     *  	  evaluationBeneficiary database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  beneficiary evaluation belongs
     *		- BeneficiaryEvaluation beneficiaryEvaluation:
     *				the beneficiaryEvaluation to be added
     *	
     *	Returns
     *		- boolean true: beneficiaryEvaluation is
     *						successfully inserted
     * 		- boolean false: insertion failed
     */
    boolean insertBeneficiaryEvaluation( int activityID,
    									 BeneficiaryEvaluation beneficiaryEvaluation ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		preparedStatement = getConnection()
    				.prepareStatement( "INSERT INTO `evaluationBeneficiary`" +
    								   " (`activityID`, `dateFiled`," +
    								   " `a1_strengths`, `a1_weaknesses`," +
    								   " `a2_strengths`, `a2_weaknesses`," +
    								   " `b1_strengths`, `b1_weaknesses`," +
    								   " `b2_strengths`, `b2_weaknesses`," +
    								   " `b3_strengths`, `b3_weaknesses`," +
    								   " `suggestions`)" +
    								   " VALUES (?, ?, ?, ?, ?, ?)" );

    		preparedStatement.setInt( 1, activityID );
    		preparedStatement.setString( 2, beneficiaryEvaluation
    										.getDateFiled().toString() );
    		for ( int i = 0; i < 10; i++ ) {
    			
				preparedStatement.setString( (i + 3), beneficiaryEvaluation
								   					  .getStrAnswers()[i] );
			}
    		preparedStatement.setString( 13, beneficiaryEvaluation
    										 .getSuggestions() );
    		preparedStatement.executeUpdate();
    		
			disconnect();
    		
    		if ( searchBeneficiaryEvaluation( activityID ) != null ) {
    			return true;
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
	
	
	
	/**
	 *  Search Beneficiary Evaluation
	 *  	- gets the beneficiaryEvaluation in a certain activity
	 *  	  from the evaluationBeneficiary database
	 *  
	 *  Parameters
	 *		- int activityID: the ID of the activity the beneficiaryEvaluation
	 *						  is from 
	 *	
	 *	Returns
	 *		- null: no beneficiaryEvaluation is found
	 * 		- BeneficiaryEvaluation: beneficiaryEvaluation is found
	 */
	BeneficiaryEvaluation searchBeneficiaryEvaluation( int activityID ) {
		
		/* Declaration of Local Variables */
		BeneficiaryEvaluation beneficiaryEvaluation = null;
		
		try {
    		/* Declaration of Block Variables */
			Statement statement;
			ResultSet resultSet;
			
			connect();
			
			statement = getConnection().createStatement();
			
			resultSet = statement
						.executeQuery( "SELECT * FROM `evaluationBeneficiary`" +
									   " WHERE `activityID` = " + activityID );
			
			while ( resultSet.next() ) {
				String[] strAnswers = new String[10];
				
				strAnswers[0] = resultSet.getString( "a1_strengths" );
				strAnswers[1] = resultSet.getString( "a1_weaknesses" );
				strAnswers[2] = resultSet.getString( "a2_strengths" );
				strAnswers[3] = resultSet.getString( "a2_weaknesses" );
				strAnswers[4] = resultSet.getString( "b1_strengths" );
				strAnswers[5] = resultSet.getString( "b1_weaknesses" );
				strAnswers[6] = resultSet.getString( "b2_strengths" );
				strAnswers[7] = resultSet.getString( "b2_weaknesses" );
				strAnswers[8] = resultSet.getString( "b3_strengths" );
				strAnswers[9] = resultSet.getString( "b3_weaknesses" );
				
				beneficiaryEvaluation = new BeneficiaryEvaluation
						( parseDate( resultSet.getDate( "dateFiled" ) ),
						  strAnswers, resultSet.getString( "suggestions" ) );
			}
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}
		finally {
	    	if ( getConnection() != null ) {
	    		disconnect();
	    	}
		}
		return beneficiaryEvaluation;
	}
    
    
    
    /**
     *  Update Beneficiary Evaluation
     *  	- update a beneficiary evaluation in the
     *  	  evaluationBeneficiary database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  beneficiary evaluation belongs
     *		- BeneficiaryEvaluation overwrite: the new version
     *				of the beneficiaryEvaluation to be updated
     *	
     *	Returns
     *		- boolean true: beneficiaryEvaluation is
     *						successfully updated
     * 		- boolean false: updating failed
     */
    boolean updateBeneficiaryEvaluation( int activityID,
    									 BeneficiaryEvaluation overwrite ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		preparedStatement = getConnection()
    				.prepareStatement( "UPDATE `evaluationBeneficiary` SET" +
    								   " `dateFiled` = ?, `a1_strengths` = ?," +
    								   " `a1_weaknesses` = ?," +
    								   " `a2_strengths` = ?," +
    								   " `a2_weaknesses` = ?," +
    								   " `b1_strengths` = ?," +
    								   " `b1_weaknesses` = ?," +
    								   " `b2_strengths` = ?," +
    								   " `b2_weaknesses` = ?," +
    								   " `b3_strengths`, = ?" +
    								   " `b3_weaknesses`, = ?" +
    								   " `suggestions` = ?)" +
    								   " WHERE `activityID` = ?" );

    		preparedStatement.setString( 1, overwrite.getDateFiled().toString() );
    		for ( int i = 0; i < 10; i++ ) {
				preparedStatement.setString( (i + 2),
											 overwrite.getStrAnswers()[i] );
			}
    		preparedStatement.setString( 12, overwrite.getSuggestions() );
    		preparedStatement.setInt( 13, activityID );
    		preparedStatement.executeUpdate();
    		
			disconnect();
    		
    		if ( searchBeneficiaryEvaluation( activityID ) != null ) {
    			return true;
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Delete Beneficiary Evaluation
     *  	- deletes a beneficiaryEvaluation from the
     *  	  evaluationBeneficiary database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  beneficiaryEvaluation belongs
     *	
     *	Returns
     *		- boolean true: beneficiaryEvaluation is successfully deleted
     * 		- boolean false: deletion failed
     */
    boolean deleteBeneficiaryEvaluation( int activityID ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		if ( searchBeneficiaryEvaluation( activityID ) != null ) {
    			preparedStatement = getConnection()
        							.prepareStatement( "DELETE FROM" +
        											   " `evaluationBeneficiary`" +
        											   " WHERE `activityID`" +
        											   " = ?" );
	    		
	    		preparedStatement.setInt( 1, activityID );
	    		preparedStatement.executeUpdate();
	    		
				disconnect();
				
	    		if ( searchBeneficiaryEvaluation( activityID ) == null ) {
	    			return true;
	    		}
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
}
